<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/29
 * Time: 19:26
 */

namespace app\admin\service;


use app\common\model\mysql\Config;

class ConfigService
{
    protected $configModel = null;

    public function __construct()
    {
        $this->configModel = new Config();
    }

    public function addConfig($data)
    {
        $res = $this->configModel->add($data);
        if (false === $res) {
            throw new \think\Exception("配置添加失败");
        }

        return true;
    }

    public function saveConfig($config_id, $data)
    {
        $res = $this->configModel->saveConfig($config_id, $data);
        if (false === $res) {
            throw new \think\Exception("配置编辑失败");
        }

        return true;
    }

    /**
     * @param $id
     * @return array|bool|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAllConfigs($id)
    {
        $res = $this->configModel->getAllConfigsByGroupId($id);
        if (empty($res)) {
            return [];
        }

        $res = $res->toArray();

        return $res;
    }

    public function getOneConfigByConfigId($config_id)
    {
        $res = $this->configModel->getOneConfigByConfigId($config_id);
        if (empty($res)) {
            return [];
        }

        return $res;
    }
}